# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=wxWidgets
_wx_basever=3.0
pkgname=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs")
pkgbase=mingw-w64-wxwidgets${_wx_basever}
pkgver=${_wx_basever}.5.1
pkgrel=16
pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("spdx:GPL-2.0-or-later AND WxWindows-exception-3.1")
url="https://www.wxwidgets.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-expat"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-xz")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
options=('strip' 'staticlibs' 'buildflags')
source=(https://github.com/wxWidgets/wxWidgets/releases/download/v${pkgver}/wxWidgets-${pkgver}.tar.bz2
        "001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch"
        "002-wxWidgets-3.0.2-relax-abi-compatibility-gcc.patch"
        "003-wxWidgets-3.0.2-fix-access-sample.patch"
        # squashed upstream commits
        # https://github.com/wxWidgets/wxWidgets/commit/c0f2f3801150ea9c6640f3fdced1d9120938f0d7
        # and
        # https://github.com/wxWidgets/wxWidgets/commit/e8a7bae0a750bcb5d8aadc45629d2c9adaf2106c
        "004-wxWidgets-3.0-clang-windows-link.patch"
        # https://github.com/wxWidgets/wxWidgets/commit/a195bf86a21e9334c68eb5e4a5b0973104553460
        "005-wxWidgets-3.0-gdiplus-math-min-max.patch"
        "006-wxWidgets-3.0-Fix-c-11-narrowing-error.patch"
        # https://github.com/wxWidgets/wxWidgets/commit/f69dbaa1aebd72652dfb6246bdeaffce4d83933d
        # with MSVC-specific build config and changes striped
        "007-wxWidgets-3.0-Introduce-msw-arm64-support.patch"
        # https://github.com/wxWidgets/wxWidgets/commit/c0c22609447c1f982500e997ba663112ac66f5f9
        "008-wxWidgets-3.0-Add-msw-manifests-for-arm-and-arm64-platforms.patch"
        # https://salsa.debian.org/freewx-team/wx/blob/wx3.0-debian/debian/patches/warn-for-compiler-abi-mismatch.patch
        "009-warn-for-compiler-abi-mismatch.patch")
sha256sums=('440f6e73cf5afb2cbf9af10cec8da6cdd3d3998d527598a53db87099524ac807'
            '03e157c427ff93c8c35b6036795e42fd980d4cb28d3ef1fef9ba6d6a9b7fff04'
            '3138f7b84bf988892f62167afc6fa640ac154b629b243d86413f7c811e508713'
            'b8684dca94b288a023a8a3d55ad56bce87570576ead71670a237d909ff1c3625'
            'b8b49b1df4a7c53d72c7c117606ac9dc44589da474ca58f0806b107469f33dcb'
            '4915d09f217270956cad3de7cf0e3fdaec4847cfda46d4b568bde26a8bd5f94a'
            '043c24cab804b972a3fa710182b19f15aff8479a34e031694c17108a64e27d3f'
            'a9f43fca5ab5c8bdcc60933a2bf00fc7ac0f9dfe715fe9eb9f426f413d0a162d'
            'd21ec13a29644547ff0d688c358a3a7795a0dc15c8e8e9761524212235db47f7'
            '53501db871290b71967af08b60aedb738c920a307ef9bd32dd19c30498732cf8')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch \
    002-wxWidgets-3.0.2-relax-abi-compatibility-gcc.patch \
    003-wxWidgets-3.0.2-fix-access-sample.patch \
    004-wxWidgets-3.0-clang-windows-link.patch \
    005-wxWidgets-3.0-gdiplus-math-min-max.patch \
    006-wxWidgets-3.0-Fix-c-11-narrowing-error.patch \
    007-wxWidgets-3.0-Introduce-msw-arm64-support.patch \
    008-wxWidgets-3.0-Add-msw-manifests-for-arm-and-arm64-platforms.patch \
    009-warn-for-compiler-abi-mismatch.patch

  # autoreconf to get updated libtool files, for aarch64 support
  pushd src/tiff
  autoreconf -fiv
  popd
}

#check() {
  #cd "${srcdir}"/build-${MSYSTEM}/samples && make
  #cd "${srcdir}"/build-${MSYSTEM}-static/samples && make

  #cd "${srcdir}"/build-${MSYSTEM}/tests && make || true
  #cd "${srcdir}"/build-${MSYSTEM}-static/tests && make || true
#}

build() {
  #CXXFLAGS+=" -std=gnu++11"
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-shared \
    --enable-iniconf \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --enable-mediactrl \
    --disable-monolithic \
    --disable-mslu \
    --disable-precomp-headers \
    --with-msw \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-zlib=sys \
    --with-expat=sys \
    --with-regex=builtin

  make #VERBOSE=1

  #CXXFLAGS+=" -std=gnu++11"
  [[ -d "${srcdir}"/build-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-static \
    --disable-shared \
    --enable-iniconf \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --enable-mediactrl \
    --disable-monolithic \
    --disable-mslu \
    --disable-precomp-headers \
    --with-msw \
    --with-opengl \
    --with-libpng=builtin \
    --with-libjpeg=builtin \
    --with-libtiff=builtin \
    --with-zlib=builtin \
    --with-expat=builtin \
    --with-regex=builtin

  make #VERBOSE=1
}

package_wxwidgets3.0-msw() {
  pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}"
            "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}"
            "${MINGW_PACKAGE_PREFIX}-wxwidgets-common"
            "${MINGW_PACKAGE_PREFIX}-wxconfig")
  conflicts=("${MINGW_PACKAGE_PREFIX}-wxWidgets")
  replaces=("${MINGW_PACKAGE_PREFIX}-wxWidgets")

  cd "${srcdir}"/build-${MSYSTEM}-static
  make DESTDIR="${pkgdir}" install
  mv ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-static}

  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  # create wx-config copy with version file suffix
  cp ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-${_wx_basever}}

  # Add missing file with exe extension (Likely bug in wxWidgets makefile)
  cp -f ${pkgdir}${MINGW_PREFIX}/bin/wxrc-${_wx_basever} ${pkgdir}${MINGW_PREFIX}/bin/wxrc-${_wx_basever}.exe

  rm    "$pkgdir"${MINGW_PREFIX}/lib/*.dll
}

package_wxwidgets3.0-msw-libs() {
  pkgdesc="wxMSW shared libraries for wxwidgets ${_wx_basever} (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-expat"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-libtiff"
           "${MINGW_PACKAGE_PREFIX}-xz"
           "${MINGW_PACKAGE_PREFIX}-zlib")

  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin

  rm    "$pkgdir"${MINGW_PREFIX}/bin/wx-config
  rm    "$pkgdir"${MINGW_PREFIX}/bin/wxrc-${_wx_basever}
  rm    "$pkgdir"${MINGW_PREFIX}/bin/*.exe
  rm -r "$pkgdir"${MINGW_PREFIX}/share
  rm -r "$pkgdir"${MINGW_PREFIX}/include
  rm -r "$pkgdir"${MINGW_PREFIX}/lib

  # License files
  cd "${srcdir}"/${_realname}-${pkgver}/docs
  install -Dm644 preamble.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/preamble.txt"
  install -Dm644 licence.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licence.txt"
  install -Dm644 licendoc.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/licendoc.txt"
  install -Dm644 lgpl.txt     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/lgpl.txt"
  install -Dm644 gpl.txt      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/gpl.txt"
  install -Dm644 xserver.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}${_wx_basever}/xserver.txt"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
